home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows (5th Edition) / Programming Windows, 5th ed. - Companion CD (097-0002183)(1999).iso / Chap14 / Bounce / Bounce.c next >
Encoding:
C/C++ Source or Header  |  1998-10-09  |  4.7 KB  |  144 lines

  1. /*---------------------------------------
  2.    BOUNCE.C -- Bouncing Ball Program
  3.                (c) Charles Petzold, 1998
  4.   ---------------------------------------*/
  5.  
  6. #include <windows.h>
  7. #define ID_TIMER    1
  8.  
  9. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  10.  
  11. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  12.                     PSTR szCmdLine, int iCmdShow)
  13. {
  14.      static TCHAR szAppName[] = TEXT ("Bounce") ;
  15.      HWND         hwnd ;
  16.      MSG          msg ;
  17.      WNDCLASS     wndclass ;
  18.  
  19.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  20.      wndclass.lpfnWndProc   = WndProc ;
  21.      wndclass.cbClsExtra    = 0 ;
  22.      wndclass.cbWndExtra    = 0 ;
  23.      wndclass.hInstance     = hInstance ;
  24.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  25.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  26.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  27.      wndclass.lpszMenuName  = NULL ;
  28.      wndclass.lpszClassName = szAppName ;
  29.      
  30.      if (!RegisterClass (&wndclass))
  31.      {
  32.           MessageBox (NULL, TEXT ("This program requires Windows NT!"),
  33.                       szAppName, MB_ICONERROR) ;
  34.           return 0 ;
  35.      }
  36.      
  37.      hwnd = CreateWindow (szAppName, TEXT ("Bouncing Ball"),
  38.                           WS_OVERLAPPEDWINDOW,
  39.                           CW_USEDEFAULT, CW_USEDEFAULT,
  40.                           CW_USEDEFAULT, CW_USEDEFAULT,
  41.                           NULL, NULL, hInstance, NULL) ;
  42.      
  43.      ShowWindow (hwnd, iCmdShow) ;
  44.      UpdateWindow (hwnd) ;
  45.      
  46.      while (GetMessage (&msg, NULL, 0, 0))
  47.      {
  48.           TranslateMessage (&msg) ;
  49.           DispatchMessage (&msg) ;
  50.      }
  51.      return msg.wParam ;
  52. }
  53.  
  54. LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
  55. {
  56.      static HBITMAP hBitmap ;
  57.      static int     cxClient, cyClient, xCenter, yCenter, cxTotal, cyTotal,
  58.                     cxRadius, cyRadius, cxMove, cyMove, xPixel, yPixel ;
  59.      HBRUSH         hBrush ;
  60.      HDC            hdc, hdcMem ;
  61.      int            iScale ;
  62.      
  63.      switch (iMsg)
  64.      {
  65.      case WM_CREATE:
  66.           hdc = GetDC (hwnd) ;
  67.           xPixel = GetDeviceCaps (hdc, ASPECTX) ;
  68.           yPixel = GetDeviceCaps (hdc, ASPECTY) ;
  69.           ReleaseDC (hwnd, hdc) ;
  70.           
  71.           SetTimer (hwnd, ID_TIMER, 50, NULL) ;
  72.           return 0 ;
  73.           
  74.      case WM_SIZE:
  75.           xCenter = (cxClient = LOWORD (lParam)) / 2 ;
  76.           yCenter = (cyClient = HIWORD (lParam)) / 2 ;
  77.           
  78.           iScale = min (cxClient * xPixel, cyClient * yPixel) / 16 ;
  79.           
  80.           cxRadius = iScale / xPixel ;
  81.           cyRadius = iScale / yPixel ;
  82.           
  83.           cxMove = max (1, cxRadius / 2) ;
  84.           cyMove = max (1, cyRadius / 2) ;
  85.           
  86.           cxTotal = 2 * (cxRadius + cxMove) ;
  87.           cyTotal = 2 * (cyRadius + cyMove) ;
  88.           
  89.           if (hBitmap)
  90.                DeleteObject (hBitmap) ;
  91.           
  92.           hdc = GetDC (hwnd) ;
  93.           hdcMem = CreateCompatibleDC (hdc) ;
  94.           hBitmap = CreateCompatibleBitmap (hdc, cxTotal, cyTotal) ;
  95.           ReleaseDC (hwnd, hdc) ;
  96.           
  97.           SelectObject (hdcMem, hBitmap) ;
  98.           Rectangle (hdcMem, -1, -1, cxTotal + 1, cyTotal + 1) ;
  99.           
  100.           hBrush = CreateHatchBrush (HS_DIAGCROSS, 0L) ;
  101.           SelectObject (hdcMem, hBrush) ;
  102.           SetBkColor (hdcMem, RGB (255, 0, 255)) ;
  103.           Ellipse (hdcMem, cxMove, cyMove, cxTotal - cxMove, cyTotal - cyMove) ;
  104.           DeleteDC (hdcMem) ;
  105.           DeleteObject (hBrush) ;
  106.           return 0 ;
  107.           
  108.      case WM_TIMER:
  109.           if (!hBitmap)
  110.                break ;
  111.           
  112.           hdc = GetDC (hwnd) ;
  113.           hdcMem = CreateCompatibleDC (hdc) ;
  114.           SelectObject (hdcMem, hBitmap) ;
  115.           
  116.           BitBlt (hdc, xCenter - cxTotal / 2,
  117.                        yCenter - cyTotal / 2, cxTotal, cyTotal,
  118.                   hdcMem, 0, 0, SRCCOPY) ;
  119.           
  120.           ReleaseDC (hwnd, hdc) ;
  121.           DeleteDC (hdcMem) ;
  122.           
  123.           xCenter += cxMove ;
  124.           yCenter += cyMove ;
  125.           
  126.           if ((xCenter + cxRadius >= cxClient) || (xCenter - cxRadius <= 0))
  127.                cxMove = -cxMove ;
  128.           
  129.           if ((yCenter + cyRadius >= cyClient) || (yCenter - cyRadius <= 0))
  130.                cyMove = -cyMove ;
  131.           
  132.           return 0 ;
  133.           
  134.      case WM_DESTROY:
  135.           if (hBitmap)
  136.                DeleteObject (hBitmap) ;
  137.           
  138.           KillTimer (hwnd, ID_TIMER) ;
  139.           PostQuitMessage (0) ;
  140.           return 0 ;
  141.      }
  142.      return DefWindowProc (hwnd, iMsg, wParam, lParam) ;
  143. }
  144.